package au.edu.unsw.editingtool.db;

import java.sql.SQLException;

import au.edu.unsw.editingtool.global.EditingToolException;

public class DBAffiliation extends DBManager {
	
	private void setupDBMapping() {
		if (MappingTable.size() > 0)
			return;
		MappingTable.put("affiliation", "aid");
		MappingTable.put("label", "label");
		MappingTable.put("oldAffiliation", "oaid");
	}
	
	public DBAffiliation() {
		this.table = "affiliation";
		this.primaryKeys = new String[]{"affiliation"};
		this.setupDBMapping();
	}
	
	public String addAffiliation(String label) throws EditingToolException, SQLException, ClassNotFoundException {
		this.setTable(this.table, "");
		String fields[] = {"affiliation"};
		String result = "1";
		if (label != null) {
			this.setFieldsCondition("label", this.setEqual(label));
			this.addField(fields);
			this.initDB();
			this.selectRecords();
			if ((this.ResultSet != null) && (this.ResultSet.next())) {
				result = this.ResultSet.getString(1);
				this.cleanResource();
			} else {
				this.setTable(this.table, "");
				this.setColumnValue("label", label);
				this.insert(true, false);
				result = this.getGenerateKey();
			}
			this.clean();
		}
		return result;
	}
	
}
